import util from '@/libs/util';
import CONSTANTS from '@/config/constants';
import Cookies from 'js-cookie';
import UEditor from '@/util/ueditor.vue';

export default {
	name: 'exportable-table',
    components: {UEditor},
	data () {
		return {
			columns: [{
				title: '序号',
				key: 'rownum',
				width: 60,
				align: 'center'
			}, {
				title: '培训班名',
				key: 'name'
			},  {
				title: '招生人数',
				key: 'applynums',
				width: 85
			},{
				title: '报名截止时间',
				key: 'applyendtime'
			}, {
				title: '培训时间',
				key: 'departtime'
			},{
				title: '操作',
				key: 'action',
				width: 300,
				align: 'center',
				render: (h, params) => {
					let isShow = 'none';
					if(params.row.releases == 'NO'){
						isShow = 'inline';
					}
					return h('div', [
						h('Button', {
							props: {
								type: 'primary',
								size: 'small'
							},
							style: {
								marginRight: '5px'
							},
							on: {
								click: () => {
									this.setPrice(params.row.id,params.row.status);
								}
							}
						}, '价格'),
						h('Button', {
							props: {
								type: 'primary',
								size: 'small'
							},
							style: {
								marginRight: '5px'
							},
							on: {
								click: () => {
									this.detail(params.row.id);
								}
							}
						}, '详情'),
						h('Button', {
							props: {
								type: 'primary',
								size: 'small'
							},
							style: {
								marginRight: '5px'
							},
							on: {
								click: () => {
									this.showEdit(params.row);
								}
							}
						}, '修改'),
						h('Button', {
							props: {
								type: 'error',
								size: 'small'
							},
							on: {
								click: () => {
									this.delete(params.row.id);
								}
							}
						}, '删除')
					]);
				}
			}],
			data: [],
			formData: {},
			viewModalVisible: false,
			saveModalVisible: false,
			priceModalVisible: false,
			isSelect:true,
			isInput:false,

			total: 0,
			pageIndex: 1,
			pageSize: 10,
			queryCon:{
				activityName:'',
				bs:'',
				cost:'',

			},
			timeList:[],
			priceList:[],
			activityId:'',
			config: {
				scaleEnabled: true,
				autoFloatEnabled: true,
				initialContent: '',
				autoClearinitialContent: true,
				initialFrameWidth: null,
				initialFrameHeight: 400,
				BaseUrl: '',
				UEDITOR_HOME_URL: '/static/ueditor/'
			}
		};
	},
	mounted() {
		this.loadList();
	},
	methods: {
		loadList() {
			util.ajax.post('/busine/activity/list', util.toFormData({
				page: this.pageIndex,
				size: this.pageSize,
				activityName: this.queryCon.activityName,
				orgId:eval('('+Cookies.get('baseUser')+')').orgId,
				bs:1,
			})).then(res => {
				var lists = res.data.data.pageInfo.list;
				for (var i = 0; i < lists.length; i++) {
					if (lists[i].type) {
						lists[i]['typename'] = lists[i].type.name;
					}
					if (lists[i].vehicle) {
						lists[i]['vehiclename'] = lists[i].vehicle.name;
					}
					lists[i]['applyendtime'] = this.getDate(lists[i].applyendtime);
					lists[i]['departtime'] = lists[i].departtime.split(',')[0];
				}
				this.data = res.data.data.pageInfo.list;
				this.total = res.data.data.pageInfo.total;
			});
		},
		detail(id) {
			util.ajax.post( '/busine/activity/detail', util.toFormData({
				id: id
			})).then(res => {
				if (res.data.data) {
					var li = res.data.data;
					if (li.activeObject) {
						li["activeObjectname"] = li.activeObject.name;
					}
					if (li.platformManage) {
						li["platformManageAppid"] = li.platformManage.appid;
					}
					li['applyendtime1'] = li['applyendtime'] = this.getDate(li.applyendtime);
					li['departendtime1'] = li['departendtime'] = this.getDate(li.departendtime);
					li['discounttime1'] = li['discounttime'] = this.getDate(li.discounttime);
					this.formData = res.data.data;
					this.viewModalVisible = true;
				} else {
					this.$Modal.error({
						title: '警告',
						content: '出错了'
					});
				}
			});
		},
		showAdd() {
			this.formData = {};
			this.formData["isidcard"] = 'NO';
			this.formData["releases"] = 'NO';
			this.formData["isinsurance"] = 0;
			this.formData["bs"]=1;
			this.timeList = [{value:''}];
			this.$refs.training_intro.setUEContent('');
			this.$refs.training_cost.setUEContent('');
			this.saveModalVisible = true;
		},
		showEdit(obj) {
			this.timeList = [];
			this.formData = {};
			util.ajax.post('/busine/activity/detail', util.toFormData({
				id: obj.id
			})).then(res => {
				if (res.data.data) {
					var li = res.data.data;
					if (li.activeObject) {
						li["activeobjectId"] = li.activeObject.id;
					}
					li['applyendtime'] = this.getDate(li.applyendtime);
					li['departendtime'] = this.getDate(li.departendtime);
					li['discounttime'] = this.getDate(li.discounttime);

					let timeArray = li.departtime.split(',');
					let timeList = [];
					for(let i=0 ; i<timeArray.length ; i++){
						timeList[i] = {value:timeArray[i]};
					}
					this.timeList = (timeList.length == 0?1:timeList);
					if(li.cover){

					}

					this.formData = res.data.data;
					this.$refs.training_intro.setUEContent(this.formData.intro);
					this.$refs.training_cost.setUEContent(this.formData.cost);
					
					this.saveModalVisible = true;
				} else {
					this.$Modal.error({
						title: '警告',
						content: '出错了'
					});
				}
			});
		},
		activitySave() {
			let formData = {};
			for (let key in this.formData) {
				formData[key] = this.formData[key];
			}
			let departtime = '';
			let timeList = this.timeList;
			for(var i=0 ; i<timeList.length ; i++){
				if(timeList[i].value != ''){
					if(departtime != '') departtime=departtime+ ',';
					departtime = departtime + this.getDate(timeList[i].value);
				}
			}
			formData['departtime'] = departtime;
			if(this.isSelect == false || formData['ledteacherId'] == ''){
				formData['ledteacherId'] = '-1';
			}
			let baseuser = eval('('+Cookies.get('baseUser')+')');
			if(baseuser.orgId){
				formData['orgId'] = baseuser.orgId;
			}
			formData['intro'] = this.$refs.training_intro.getUEContent();
			formData['cost'] = this.$refs.training_cost.getUEContent();
			util.ajax.post('/busine/activity/save', util.toFormData(formData)).then(res => {
				if (res.data.message === 'SUCCESS') {
					this.saveModalVisible = false;
					this.$Message.success('保存成功！');
					this.loadList();
				} else {
					this.$Message.warning(res.data.message || '未知错误！');
				}
			});
		},
		delete(id) {
			this.$Modal.confirm({
				title: '提示',
				content: '<p>确定删除？</p>',
				onOk: () => {
					util.ajax.post('/busine/activity/delete', util.toFormData({
						id: id
					})).then(res => {
						if (res.data.message === 'SUCCESS') {
							this.$Message.success('删除成功！');
							this.loadList();
						}
					});
				},
				onCancel: () => {
				}
			});
		},
		getDate(data) {
			let myDate;

			if (data instanceof Date){
				myDate = data;
			} else if (typeof(data) == 'number') {
				myDate = new Date(data);
			} else {
				return data;
			}

			let Y = myDate.getFullYear(),
				M = myDate.getMonth() + 1,
				D = myDate.getDate(),
				H = myDate.getHours(),
				Min = myDate.getMinutes(),
				S = myDate.getSeconds();

			if (M < 10) {
				M = '0' + M;
			}
			if (D < 10) {
				D = '0' + D;
			}
			if (H < 10) {
				H = '0' + H;
			}
			if (Min < 10) {
				Min = '0' + Min;
			}
			if (S < 10) {
				S = '0' + S;
			}
			return Y + '-' + M + '-' + D;
		},
		addTime() {
			const time = {value:''};
			const timeList = this.timeList;
			this.timeList.splice(timeList.length, 0, time);
		},
		delTime() {
			const timeList = this.timeList;
			if(timeList.length>0){
				this.timeList.splice(timeList.length-1, 1);
			}
		},
		refresh(){
			this.queryCon = {
				activityName:'',
				csst:'',
			};
			this.loadList();
		},
		setPrice(id,status){
			this.activityId = id;
			let _this = this;
			util.ajax.post('/busine/price/listByActivity', util.toFormData({
				id: id,
				status:status
			})).then(res => {
					if (res.data.message === 'SUCCESS') {
						if(res.data.data){
							_this.priceList = res.data.data;
							if(_this.priceList.length == 0){
								_this.priceList.splice(0, 0, {
									name:'',
									money:0,
									peoplenums:0
								});
							}
							this.priceModalVisible = true;
						}
					} else {
						this.$Message.warning(res.data.message || '未知错误！');
					}

			})
		},
		addPrice(){
			const price = {
				name:'',
				money:0,
				peoplenums:0
			};
			const priceList = this.priceList;
			this.priceList.splice(priceList.length, 0, price);
		},
		delPrice(){
			const priceList = this.priceList;
			if(priceList.length>0){
				this.priceList.splice(priceList.length-1, 1);
			}
			if(priceList.length==0){
				this.priceList.splice(0, 0, {
					name:'',
					money:0,
					peoplenums:0
				});
			}
		},
		priceSave(){
			let _this = this;
			let priceList = [];
			for(let i=0 ; i<_this.priceList.length ; i++){
				if(_this.priceList[i].name!="" && _this.priceList[i].money!=""){
					priceList.splice(priceList.length-1,0,_this.priceList[i]);
				}
			}
			util.ajax.post('/busine/price/saveByActivity', util.toFormData({
				id:_this.activityId,
				listStr:JSON.stringify(priceList)
			})).then(res => {
				if (res.data.message === 'SUCCESS') {
					this.saveModalVisible = false;
					this.$Message.success('保存成功！');
					this.loadList();
				} else {
					this.$Message.warning(res.data.message || '未知错误！');
				}
			});
			this.priceModalVisible = false;
		},
		priceCancel(){
			this.priceModalVisible = false;
		},
		destroyed() {
			this.saveModalVisible = false
		},
		teacherSelect(value){
			this.formData.ledteacher = value
			let list = this.ledTeacherList.filter(item => item.name.indexOf(value) > -1);
			if(list.length==0){
				this.isSelect = false;
				this.isInput = true;
				this.formData.ledteacherId = '';
			}
		},
		teacherInput(data){
			if(!(data.data)){
				this.isSelect = true;
				this.isInput = false;
			}
		}
	}
};